//
//  Manager.m
//  WangYiNews
//
//  Created by lanou3g on 15/10/12.
//  Copyright (c) 2015年 zhengming. All rights reserved.
//

#import "Manager.h"

@interface Manager ()

@end

@implementation Manager

+ (Manager *)share
{
    static Manager *manager ;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    return manager;
}

- (void)setupFinderDataWithBlock:(void(^)())block
{
    NSURL *url = [NSURL URLWithString:kFinderUrl];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"get"];
        NSURLResponse *response = [[NSURLResponse alloc] init];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        self.finderBannerArray = [NSMutableArray array];
        NSArray *banners = [dic valueForKey:@"banner"];
        for (NSDictionary *dic in banners) {
            FinderModel *model = [[FinderModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.finderBannerArray addObject:model];
        }
        
        self.finderRecommendArray = [NSMutableArray array];
        NSArray *recommends = [dic valueForKey:@"recommend"];
        for (NSDictionary *dic in recommends) {
            FinderModel *model = [[FinderModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.finderRecommendArray addObject:model];
        }
        
        self.finderContextArray = [NSMutableArray array];
        NSArray *streams = [dic valueForKey:@"stream"];
        for (NSDictionary *dic in streams) {
            NSArray *contexts = [dic valueForKey:@"context"];
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *conDic in contexts) {
                FinderModel *model = [[FinderModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [model setValuesForKeysWithDictionary:conDic];
                [tempArray addObject:model];
            }
            [self.finderContextArray addObject:tempArray];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            block();
        });
    });
}

// 加载TV信息
- (void)setupTVDataWithBlock:(void(^)())block
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSURL *url = [NSURL URLWithString:kVideoUrl];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];
        [request setHTTPMethod:@"GET"];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSDictionary *tempDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *tempArray = tempDic[@"V9LG4B3A0"];
        self.TVArray = [NSMutableArray array];
        for (NSDictionary *dic in tempArray) {
            TVModel *model = [[TVModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.TVArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            block();
        });
    });
}

// 加载Video信息
- (void)setupVideoDataWithBlock:(void(^)())block
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSURL *url = [NSURL URLWithString:kTVUrl];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30.0];
        [request setHTTPMethod:@"GET"];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSArray *tempArray = dic[@"cList"];
        self.videoArray = [NSMutableArray array];
        for (NSDictionary *dicOne in tempArray) {
            NSMutableArray *array = [NSMutableArray array];
            NSArray *tempArr = dicOne[@"tList"];
            for (NSDictionary *dicTwo in tempArr) {
                VideoModel *model = [[VideoModel alloc] init];
                [model setValuesForKeysWithDictionary:dicOne];
                [model setValuesForKeysWithDictionary:dicTwo];
                NSDictionary *dicThree = dicTwo[@"radio"];
                [model setValuesForKeysWithDictionary:dicThree];
                [array addObject:model];
            }
            [self.videoArray addObject:array];
        }
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            block();
        });
    });
}



@end
